package greet

import (
	"github.com/gin-gonic/gin"

	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/internal/domain"
	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/internal/logic"
	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/internal/svc"
	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/pkg/httpx"
)

type User struct {
	svcCtx *svc.ServiceContext
	user   logic.User
}

func NewUser(svcCtx *svc.ServiceContext, user logic.User) *User {
	return &User{
		svcCtx: svcCtx,
		user:   user,
	}
}

func (h *User) InitRegister(engine *gin.Engine) {
	g0 := engine.Group("user/v1")
	g0.GET("/user/from/:name", h.UserHandler)
	g0.GET("/user/get", h.NoUserResponseHandler)

	g1 := engine.Group("user/v2")
	g1.GET("/user/from2/:name", h.UserHandler2)
	g1.GET("/user/get2", h.NoUserResponseHandler2)
}

func (h *User) UserHandler(ctx *gin.Context) {
	var req domain.Request
	if err := ctx.ShouldBind(&req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}

	res, err := h.user.UserHandler(ctx, &req)
	if err != nil {
		httpx.FailWithErr(ctx, err)
	} else {
		httpx.OkWithData(ctx, res)
	}
}

func (h *User) NoUserResponseHandler(ctx *gin.Context) {
	var req domain.Request
	if err := ctx.ShouldBind(&req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}
	_ = req
}

// 测试
func (h *User) UserHandler2(ctx *gin.Context) {
	var req domain.Request
	if err := ctx.ShouldBind(&req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}

	res, err := h.user.UserHandler2(ctx, &req)
	if err != nil {
		httpx.FailWithErr(ctx, err)
	} else {
		httpx.OkWithData(ctx, res)
	}
}

func (h *User) NoUserResponseHandler2(ctx *gin.Context) {
	var req domain.Request
	if err := ctx.ShouldBind(&req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}
	_ = req
}
